package com.wandian.weather.mvvm.realweather;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mylib.libcore.adapter.BasePagerAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.TimeUtils;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.weather.R;
import com.wandian.weather.databinding.WeatherActivityRealWatherBinding;
import com.wandian.weather.mvvm.realweather.rainfallsituation.RainSituationFragment;
import com.wandian.weather.mvvm.realweather.realtimefragment.RealtimeWeatherFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 实时天气
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_WEATHER_REAL_WEATHER)
public class RealWeatherActivity extends BaseMVVMActivity<RealWeatherViewModel> {
    private WeatherActivityRealWatherBinding dataBinding;
    private BasePagerAdapter myAdapter;
    private List<String> titleList=new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.weather_activity_real_wather);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        dataBinding.bindSysToolBar.toolBarTvTitle.setText("重庆");
        initTabPage();
        initClink();
        //更具时间更换背景
        TimeUtils.customHomeBgByTime(this,dataBinding.llSstq);
    }

    //初始化tabpage
    private void initTabPage() {
        titleList.clear();
        fragments.clear();
        titleList.add("实时天气");
        titleList.add("降雨情况");

        fragments.add(RealtimeWeatherFragment.newInstance(titleList.get(0).trim()));
        fragments.add(RainSituationFragment.newInstance(titleList.get(1).trim()));

        myAdapter = new BasePagerAdapter(getSupportFragmentManager(), fragments,titleList);
        if(UIUtils.isEmpty(dataBinding.viewPager.getAdapter())){
            dataBinding.viewPager.setAdapter(myAdapter);
            dataBinding.viewPager.setOffscreenPageLimit(titleList.size());
            dataBinding.tabLayout.setViewPager(dataBinding.viewPager);
        }
        if(!UIUtils.isEmpty(myAdapter)){
            myAdapter.notifyDataSetChanged();
        }
        //预加载数量
        dataBinding.viewPager.setOffscreenPageLimit(1);
        dataBinding.tabLayout.setViewPager(dataBinding.viewPager);
        dataBinding.tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        dataBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void initClink(){
        dataBinding.bindSysToolBar.toolBarIvBack.setOnClickListener(v -> {
            finish();
        });

    }



}
